package co.baiku.boot.core.orm.request;

import co.baiku.boot.common.tools.JsonTools;
import co.baiku.boot.common.tools.StringTools;

import java.util.*;

public class PagingRequest {
    /**
     * 页码,默认1
     */
    protected int pageNo = 1;
    /**
     * 每页条数,默认15
     */
    protected int pageSize = 15;
    /**
     * 查询条件
     */
    protected Map<String, Object> search = new HashMap<>();
    /**
     * 查询组
     */
    protected List<SearchGroup> searchGroup = new ArrayList<>();
    /**
     * 排序依据
     */
    protected Map<String, String> sortBy = new LinkedHashMap<>();

    public static PagingRequest of(int pageNo, int pageSize) {
        return new PagingRequest(pageNo, pageSize);
    }

    public PagingRequest() {
    }

    public PagingRequest(int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
    }

    /**
     * get动态请求
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @param query    查询规则,eg:query=EQ_name__张三
     * @param sort     排序规则,eg:sort=id_asc,name_desc
     */
    public PagingRequest(int pageNo, int pageSize, String query, String sort) {
        setPageNo(pageNo == 0 ? 1 : pageNo);
        setPageSize(pageSize == 0 ? 15 : pageSize);
        if (StringTools.isNotBlank(sort)) {
            String[] sortArr = sort.split(StringTools.COMMA);
            Map<String, String> sortByMap = new HashMap<>();
            for (String s : sortArr) {
                if (!s.contains(StringTools.UNDERLINE)) {
                    s += "_asc";
                }
                String option = s.substring(s.lastIndexOf(StringTools.UNDERLINE));
                if ("_asc/_desc".contains(option)) {
                    sortByMap.put(s.substring(0, s.indexOf(StringTools.UNDERLINE)), "_desc".equals(option) ? "desc" : "asc");
                }
            }
            setSortBy(sortByMap);
        }
        if (StringTools.isNotBlank(query)) {
            String[] queryArr = query.split(StringTools.COMMA);
            Map<String, Object> search = new HashMap<>();
            for (String q : queryArr) {
                String[] item = q.split("__");
                if (item.length == 2) {
                    search.put(item[0], item[1]);
                }
            }
            setSearch(search);
        }
    }

    /**
     * 增加排序规则
     *
     * @param property  排序字段
     * @param direction 排序规则
     * @return
     */
    public PagingRequest addSort(String property, String direction) {
        sortBy.put(property, direction);
        return this;
    }

    /**
     * 增加查询规则
     *
     * @param expression 表达式
     * @param value      值
     * @return
     */
    public PagingRequest addSearch(String expression, Object value) {
        search.put(expression, value);
        return this;
    }


    public PagingRequest addSearchGroup(Map<String, Object> search, RequestFilter.Operator operator) {
        searchGroup.add(new SearchGroup(search, operator));
        return this;
    }


    public <T> T getParameter(String key, Class<T> tClass) {
        String o = (String) search.get(key);
        if (o == null) {
            return null;
        }
        return JsonTools.jsonToBean(o, tClass);
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Map<String, String> getSortBy() {
        return sortBy;
    }

    public void setSortBy(Map<String, String> sortBy) {
        this.sortBy = sortBy;
    }

    public Map<String, Object> getSearch() {
        return search;
    }

    public void setSearch(Map<String, Object> search) {
        this.search = search;
    }

    public List<SearchGroup> getSearchGroup() {
        return searchGroup;
    }

    public void setSearchGroup(List<SearchGroup> searchGroup) {
        this.searchGroup = searchGroup;
    }
}
